<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多语言翻译测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        select, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #007bff;
        }
        .language-info {
            font-size: 12px;
            color: #666;
            margin-top: 10px;
        }
        .error {
            background-color: #f8d7da;
            border-left-color: #dc3545;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🌐 多语言翻译测试</h1>
        
        <div class="form-group">
            <label for="sourceLang">源语言:</label>
            <select id="sourceLang">
                <option value="auto">自动检测</option>
                <option value="zh">中文</option>
                <option value="en">英语</option>
                <option value="ja">日语</option>
                <option value="ko">韩语</option>
                <option value="fr">法语</option>
                <option value="es">西班牙语</option>
                <option value="it">意大利语</option>
                <option value="de">德语</option>
                <option value="tr">土耳其语</option>
                <option value="ru">俄语</option>
                <option value="pt">葡萄牙语</option>
                <option value="vi">越南语</option>
                <option value="id">印尼语</option>
                <option value="th">泰语</option>
                <option value="ms">马来语</option>
            </select>
        </div>

        <div class="form-group">
            <label for="targetLang">目标语言:</label>
            <select id="targetLang">
                <option value="zh" selected>中文</option>
                <option value="en">英语</option>
                <option value="ja">日语</option>
                <option value="ko">韩语</option>
                <option value="fr">法语</option>
                <option value="es">西班牙语</option>
                <option value="it">意大利语</option>
                <option value="de">德语</option>
                <option value="tr">土耳其语</option>
                <option value="ru">俄语</option>
                <option value="pt">葡萄牙语</option>
                <option value="vi">越南语</option>
                <option value="id">印尼语</option>
                <option value="th">泰语</option>
                <option value="ms">马来语</option>
            </select>
        </div>

        <div class="form-group">
            <label for="sourceText">要翻译的文本:</label>
            <textarea id="sourceText" placeholder="请输入要翻译的文本...">Hello, how are you today?</textarea>
        </div>

        <button id="translateBtn">翻译</button>

        <div id="result" class="result" style="display: none;">
            <strong>翻译结果:</strong>
            <div id="translatedText"></div>
            <div class="language-info" id="languageInfo"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:3001';

        // 等待DOM加载完成后绑定事件
        document.addEventListener('DOMContentLoaded', function() {
            // 绑定按钮点击事件
            document.getElementById('translateBtn').addEventListener('click', translate);

            // 回车键翻译
            document.getElementById('sourceText').addEventListener('keypress', function(e) {
                if (e.key === 'Enter' && e.ctrlKey) {
                    translate();
                }
            });
        });

        async function translate() {
            const sourceText = document.getElementById('sourceText').value.trim();
            const sourceLang = document.getElementById('sourceLang').value;
            const targetLang = document.getElementById('targetLang').value;
            const translateBtn = document.getElementById('translateBtn');
            const resultDiv = document.getElementById('result');
            const translatedTextDiv = document.getElementById('translatedText');
            const languageInfoDiv = document.getElementById('languageInfo');

            if (!sourceText) {
                alert('请输入要翻译的文本');
                return;
            }

            // 禁用按钮并显示加载状态
            translateBtn.disabled = true;
            translateBtn.textContent = '翻译中...';
            resultDiv.style.display = 'none';

            try {
                const response = await fetch(`${API_BASE}/translate`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        text: sourceText,
                        source_lang: sourceLang,
                        target_lang: targetLang
                    })
                });

                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }

                const data = await response.json();
                
                // 显示结果
                translatedTextDiv.textContent = data.translated_text;
                languageInfoDiv.textContent = `从 ${getLanguageName(sourceLang)} 翻译到 ${getLanguageName(targetLang)}`;
                resultDiv.className = 'result';
                resultDiv.style.display = 'block';

            } catch (error) {
                console.error('翻译失败:', error);
                translatedTextDiv.textContent = `翻译失败: ${error.message}`;
                languageInfoDiv.textContent = '';
                resultDiv.className = 'result error';
                resultDiv.style.display = 'block';
            } finally {
                // 恢复按钮状态
                translateBtn.disabled = false;
                translateBtn.textContent = '翻译';
            }
        }

        function getLanguageName(code) {
            const languages = {
                'auto': '自动检测',
                'zh': '中文',
                'en': '英语',
                'ja': '日语',
                'ko': '韩语',
                'fr': '法语',
                'es': '西班牙语',
                'it': '意大利语',
                'de': '德语',
                'tr': '土耳其语',
                'ru': '俄语',
                'pt': '葡萄牙语',
                'vi': '越南语',
                'id': '印尼语',
                'th': '泰语',
                'ms': '马来语'
            };
            return languages[code] || code;
        }

        // 绑定按钮点击事件
        document.getElementById('translateBtn').addEventListener('click', translate);

        // 回车键翻译
        document.getElementById('sourceText').addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && e.ctrlKey) {
                translate();
            }
        });
    </script>
</body>
</html> 